package com.example.springbootdatamock.mock;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import com.example.springbootdatamock.mapper.MysqlMapper;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Component
@RestController
public class MysqlMocker
        implements Runnable {
    @Autowired
    private MysqlMapper mapper;
    @Value("${sleepMs.mysqlMockSleep}")
    private Integer mockSleep;

    @RequestMapping("/mysqlMock")
    public void run() {
        this.mapper.createTable();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println("mysqlMock start...");

        while (true) {
            this.mapper.insertData(LocalDateTime.now().format(dateTimeFormatter),
                    Integer.valueOf(RandomUtils.nextInt(1, 8)),
                    Integer.valueOf(RandomUtils.nextInt(1, 4)));

            System.out.println("mysqlMock insert data...");
/*            try {
                Thread.sleep(this.mockSleep.intValue());

            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/
        }
    }


}



 